package com.ms.controller.user;

import com.ms.context.BaseContext;
import com.ms.domain.entity.Message;
import com.ms.result.Result;
import com.ms.service.MessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/messages")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "C端消息相关接口")
public class MessageController {
    private final MessageService messageService;

    /**
     * 分页获取当前用户的消息列表
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 消息列表
     */
    @GetMapping
    @Operation(summary = "分页获取当前用户的分页消息列表")
    public Result<List<Message>> getUserMessages(int pageNum, int pageSize) {
        Integer userId = BaseContext.getCurrentId();
        log.info("获取当前用户的消息列表，用户ID：{}，页码：{}，页大小：{}", userId, pageNum, pageSize);
        return Result.success(messageService.getUserMessages(userId, pageNum, pageSize));
    }

    /**
     * 获取未读消息数量
     * @return 未读消息数量
     */
    @GetMapping("/unread-count")
    @Operation(summary = "获取未读消息数量")
    public Result<Integer> getUnreadCount() {
        Integer userId = BaseContext.getCurrentId();
        return Result.success(messageService.getUnreadMessageCount(userId));
    }

    /**
     * 标记单条消息为已读
     * @param id 消息ID
     * @return 操作结果
     */
    @PutMapping("/{id}/read")
    @Operation(summary = "标记单条消息为已读")
    public Result<Void> markAsRead(@PathVariable Integer id) {
        log.info("标记单条消息为已读，消息ID：{}", id);
        messageService.markAsRead(id);
        return Result.success();
    }

    /**
     * 标记所有消息为已读
     * @return 操作结果
     */
    @PutMapping("/read-all")
    @Operation(summary = "标记所有消息为已读")
    public Result<Void> markAllAsRead() {
        Integer userId = BaseContext.getCurrentId();
        messageService.markAllAsRead(userId);
        return Result.success();
    }

    /**
     * 删除某条消息
     * @param id 消息ID
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除某条消息")
    public Result<Void> deleteMessage(@PathVariable Integer id) {
        messageService.deleteMessage(id);
        return Result.success();
    }

    /**
     * 删除所有消息
     */
    @DeleteMapping
    @Operation(summary = "删除所有消息")
    public Result<Void> deleteAllMessages() {
        messageService.deleteAllMessages();
        return Result.success();
    }
}
